import axios from 'axios';
import { Toast } from 'vant';
import router from '@/router'
let domain = '';
// 
if (process.env.NODE_ENV === 'production') {
    domain = 'https://www.mof999.com/';
}
axios.defaults.withCredentials = true;
let CancelToken = axios.CancelToken;
let source = CancelToken.source();
// const objToUrl = obj => {
//     let arr = [];
//     for (let i in obj) {
//         if (obj.hasOwnProperty(i)) {
//             arr.push(encodeURIComponent(i) + "=" + encodeURIComponent(obj[i]));
//         }
//     }
//     return '?' + arr.join("&");
// };

export const request = (method, _module, params = {}, headers = {}, that) => {
    //如果是GET方式 ， 则拼接URL参数；
    // if ('GET' === method) _module += objToUrl(params);
    if ('GET' === method) {
        return axios.get(domain + _module, { params: params }, {
            cancelToken: new axios.CancelToken(function executor(c) { // 设置 cancel token
                if (that) {
                    that.source = c;
                    // console.log(that.source)
                }
            })
        })
    } else {
        return axios.post(domain + _module, params, {
            transformRequest: data => {
                var str = [];
                for (var i in data) {
                    str.push(
                        encodeURIComponent(i) + "=" + encodeURIComponent(data[i])
                    );
                }
                return str.join("&");
            },
            cancelToken: new axios.CancelToken(function executor(c) {
                // 设置 cancel token
                if (that) {
                    that.source = c;
                }
            })
        })
    }
    // return axios.request({
    //     url: domain + _module,
    //     method: 'GET' === method ? 'GET' : 'POST',
    //     headers: Headers,
    //     params: params,
    //     // data: 'GET' === method ? params : '',
    //     transformRequest: data => {
    //         var str = [];
    //         for (var i in data) {
    //             str.push(
    //                 encodeURIComponent(i) + "=" + encodeURIComponent(data[i])
    //             );
    //         }
    //         return str.join("&");
    //     },
    //     cancelToken: new axios.CancelToken(function executor(c) { // 设置 cancel token


    //     })
    // })

};
axios.defaults.withCredentials = true;

axios.interceptors.response.use(
    response => {
        let msg = response.data
            // if (response.config.method == 'post') {
            //     if (msg.code != '1000') {
            //         Toast.fail(msg.reason);
            //     }
            // }
        switch (msg.code) {
            case 1000:
                return msg;
            case 1100: //请您先登录
                Toast.fail(msg.reason);
                setTimeout(() => {
                    router.push("/user/login")
                }, 500);
                window.localStorage.clear();
                return msg;
            default:
                Toast.fail(msg.reason);
                return msg;
                break;
        }

    },
    error => {
        Toast.fail(error);
        // Message.error({
        //     message: error.message,
        //     duration: 5 * 1000,
        //     closable: true
        // });
        return Promise.reject(error);
    }
);